/**
 * Created by jinxs on 2015.08.28.
 */
(function($){
    //全屏模块
    var fullScreen = {
        supports: false, eventName: "", prefix: "", prefixes: "webkit moz o ms khtml".split(" "),
        isFullScreen: function () {
        }, requestFullScreen: function () {
        }, cancelFullScreen: function () {
        }
    };
    if (typeof document.cancelFullScreen != "undefined") {
        fullScreen.supports = true;
    } else {
        for (var i = 0; i < fullScreen.prefixes.length; i++) {
            fullScreen.prefix = fullScreen.prefixes[i];
            if (typeof document[fullScreen.prefix + "CancelFullScreen"] != "undefined") {
                fullScreen.supports = true;
                break;
            }
        }
    }
    if (fullScreen.supports) {
        fullScreen.eventName = fullScreen.prefix + "fullscreenchange";
        fullScreen.isFullScreen = function () {
            switch (this.prefix) {
                case "":
                    return document.fullScreen;
                case "webkit":
                    return document.webkitIsFullScreen;
                default:
                    return document[this.prefix + "FullScreen"];
            }
        };
        fullScreen.requestFullScreen = function (elem) {
            return (this.prefix === "") ? elem.requestFullScreen() : elem[this.prefix + "RequestFullScreen"]();
        };
        fullScreen.cancelFullScreen = function (elem) {
            return (this.prefix === "") ? document.cancelFullScreen() : document[this.prefix + "CancelFullScreen"]();
        };
    }
    $.uiFullScreen = {
        supportsFullScreen: fullScreen.supports
        , fullScreenEventName: fullScreen.eventName
        , fullScreen: fullScreen
        , isFullScreen: function () {
            return fullScreen.isFullScreen();
        }
        , requestFullScreen: function (selector) {
            if (selector == null || selector == undefined) {
                selector = document.documentElement;
            }
            selector = $(selector);
            return selector.each(function () {
                if (fullScreen.supports) {
                    fullScreen.requestFullScreen(this);
                }
            });
        }
        , cancelFullScreen: function (selector) {
            if (selector == null || selector == undefined) {
                selector = document.documentElement;
            }
            selector = $(selector);
            return selector.each(function () {
                if (fullScreen.supports) {
                    fullScreen.cancelFullScreen(this);
                }
            });
        }
        , toggleFullScreen: function (selector) {
            if (selector == null || selector == undefined) {
                selector = document.documentElement;
            }
            selector = $(selector);
            return selector.each(function () {
                if (fullScreen.supports) {
                    if ($.util.isFullScreen()) {
                        fullScreen.cancelFullScreen(this);
                    } else {
                        fullScreen.requestFullScreen(this);
                    }
                }
            });
        }
    };
})(jQuery);
